import { _decorator, Component, Sprite, SpriteFrame, UITransform } from 'cc'
const { ccclass } = _decorator
import { TILE_WIDTH, TILE_HEIGHT } from '../../utils/constant'
import { TILE_TYPE_ENUM } from '../../Enums'

@ccclass('TileManager')
export class TileManager extends Component {
  type: TILE_TYPE_ENUM
  moveable: boolean
  turnable: boolean

  init(type: TILE_TYPE_ENUM, spriteFrame: SpriteFrame, i: number, j: number) {
    this.type = type

    /**
     * 墙体不能移动，也不能转向
     * 包括：
     * 1. 墙体行 WALL_ROW
     * 2. 墙体列 WALL_COLUMN
     * 3. 墙体左上角 WALL_LEFT_TOP
     * 4. 墙体右上角 WALL_RIGHT_TOP
     * 5. 墙体左下角 WALL_LEFT_BOTTOM
     * 6. 墙体右下角 WALL_RIGHT_BOTTOM
     */
    if(this.type.includes('WALL')) {
      this.moveable = false
      this.turnable = false
    } else if (this.type.includes('CLIFF')) {
      // 包括 CLIFF_CENTER、CLIFF_LEFT、CLIFF_RIGHT
      this.moveable = false
      this.turnable = true
    } else {
      // FLOOR
      this.moveable = true
      this.turnable = true
    }

    const sprite = this.addComponent(Sprite)
    sprite.spriteFrame = spriteFrame

    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH, TILE_HEIGHT)
    // 设置节点位置,y轴需要取反,因为游戏设置左上角为原点，而cocos设置左下角为原点
    this.node.setPosition(i * TILE_WIDTH, -j * TILE_HEIGHT)
  }
}
